from rest_framework import serializers

from apps.comments.models import Comment
from apps.users.models import User
from utils.models import BaseModelSerializer


class CommentUserModelSerializer(BaseModelSerializer):
    class Meta:
        model = User
        fields = ['decorate_avatar', 'username', 'id']
        read_only_fields = ('decorate_avatar', 'username', 'id')


class TeacherCommentModelSerializer(BaseModelSerializer):
    user_id = serializers.IntegerField(write_only=True)
    user = CommentUserModelSerializer(read_only=True)
    teacher_id = serializers.IntegerField()

    class Meta:
        model = Comment
        fields = ['user', 'content', 'teacher_id', 'user_id', 'rating', 'id', 'like_users', 'create_time']
        read_only_fields = ['like_users', 'create_time']


class ArticleCommentModelSerializer(BaseModelSerializer):
    user_id = serializers.IntegerField(write_only=True)
    user = CommentUserModelSerializer(read_only=True)
    article_id = serializers.IntegerField()
    replies_count = serializers.IntegerField(read_only=True)
    parent_id = serializers.IntegerField(required=False)

    class Meta:
        model = Comment
        fields = ['user', 'content', 'article_id', 'user_id', 'id', 'like_users', 'create_time', 'replies_count',
                  'parent_id']
        read_only_fields = ['like_users', 'create_time', 'user', 'replies_count']
